package CompleteFutureDemo;

import common.CalcTask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;

/**
 * @author chao
 * @create 2022-02-06-16:59
 */
public class ThenCombineTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Double> cf = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult());
        CompletableFuture<Double> cf2 = CompletableFuture.supplyAsync(() -> new CalcTask().RunWithResult());
        CompletableFuture<Double> cf3 = cf.thenCombine(cf2, (r1, r2) -> {
            System.out.println("cf3 执行 -> " + System.currentTimeMillis());
            return r1 + r2;
        });
        CompletableFuture<Void> cf4 = cf.thenAcceptBoth(cf2, (r1, r2) -> {
            System.out.println("cf4处理 cf和 cf2 的结果为 -> " + (r1 - r2));
        });
        CompletableFuture<Void> cf5 = cf4.runAfterBoth(cf3, () -> {
            System.out.println("cf5 执行");
        });
        System.out.println("cf 结果 -> "+cf.get());
        System.out.println("cf5 执行 -> " +cf5.get());

    }
}
